﻿using EShop.Net.IRepository;
using EShop.Net.IServices;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace EShop.Net.Services
{
    /// <summary>
    /// 服务仓储通用接口类   实现
    /// 只提供了基础的增删改查，涉及复杂增删改查，请在仓储层写相关方法
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseServices<T> : IBaseServices<T> where T : class, new()
    {
        public IBaseRepository<T> BaseDal;

        /// <summary>
        /// 根据ID，查询单条数据(同步)
        /// </summary>
        /// <param name="pkValue">ID</param>
        /// <returns>泛型实体</returns>
        public T QueryById(object pkValue)
        {
            return this.BaseDal.QueryById(pkValue);
        }

        /// <summary>
        /// 根据ID，查询单条数据(异步)
        /// </summary>
        /// <param name="objId">ID</param>
        /// <returns>泛型实体</returns>
        public async Task<T> QueryByIdAsync(object objId)
        {
            return await this.BaseDal.QueryByIdAsync(objId);
        }

        /// <summary>
        /// 查询所有数据，无分页小小使用(同步)
        /// </summary>
        /// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
        /// <returns></returns>
        public List<T> Query(bool blUseNoLock = false)
        {
            return this.BaseDal.Query(blUseNoLock);
        }

        /// <summary>
        /// 查询所有数据，无分页小小使用(异步)
        /// </summary>
        /// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
        /// <returns></returns>
        public async Task<List<T>> QueryAsync(bool blUseNoLock = false)
        {
            return await this.BaseDal.QueryAsync(blUseNoLock);
        }

        /// <summary>
        /// 根据条件查询数据(同步)
        /// </summary>
        /// <param name="predicate">条件表达式树</param>
        /// <param name="orderBy">排序</param>
        /// <returns>泛型实体集合</returns>
        public List<T> QueryListByClause(Expression<Func<T, bool>> predicate, string orderBy = "")
        {
            return this.BaseDal.QueryListByClause(predicate, orderBy);
        }

        /// <summary>
        /// 根据条件查询数据(异步)
        /// </summary>
        /// <param name="predicate">条件表达式树</param>
        /// <param name="orderBy">排序</param>
        /// <returns>泛型实体集合</returns>
        public async Task<List<T>> QueryListByClauseAsync(Expression<Func<T, bool>> predicate, string orderBy = "")
        {
            return await this.BaseDal.QueryListByClauseAsync(predicate, orderBy);
        }

        /// <summary>
        /// 写入实体数据(同步)
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns>受影响行数</returns>
        public int Insert(T entity)
        {
            return this.BaseDal.Insert(entity);
        }

        /// <summary>
        /// 写入实体数据(异步)
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns>受影响行数</returns>
        public async Task<int> InsertAsync(T entity)
        {
            return await this.BaseDal.InsertAsync(entity);
        }

        /// <summary>
        /// 更新实体数据(同步)
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns>成功或失败</returns>
        public bool Update(T entity)
        {
            return this.BaseDal.Update(entity);
        }

        /// <summary>
        /// 更新实体数据(异步)
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns>成功或失败</returns>
        public async Task<bool> UpdateAsync(T entity)
        {
            return await this.BaseDal.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除指定ID的数据(同步)
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>成功或失败</returns>
        public bool DeleteById(object id)
        {
            return this.BaseDal.DeleteById(id);
        }

        /// <summary>
        /// 删除指定ID的数据(异步)
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>成功或失败</returns>
        public async Task<bool> DeleteByIdAsync(object id)
        {
            return await this.BaseDal.DeleteByIdAsync(id);
        }

        /// <summary>
        /// 执行预处理sql语句并返回List<T>
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">预处理对象</param>
        /// <returns></returns>
        public List<T> SqlQueryPara(string sql, List<SugarParameter> parameters)
        {
            return this.BaseDal.SqlQueryPara(sql, parameters);
        }

        /// <summary>
        /// 执行sql语句并返回List<T>
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <returns></returns>
        public async Task<List<T>> SqlQuery(string sql)
        {
            return await this.BaseDal.SqlQuery(sql);
        }
    }
}
